//
//  StartNewModule.m
//  sxwphone
//
//  Created by caijinglong on 2017/7/16.
//  Copyright © 2017年 Facebook. All rights reserved.
//

#import "RouteModule.h"
#import "RootViewController.h"
#import "React/RCTBundleURLProvider.h"
#import "React/RCTRootView.h"
#import "RCTViewDelegate.h"

@implementation RouteModule
static NSMutableArray *ctlArray;

RCT_EXPORT_MODULE();

RCT_EXPORT_METHOD(newPageWithJsName:
                  (NSString *) jsName
                  moduleName:
                  (NSString *) moduleName params:(NSDictionary *) params) {
  NSLog(@"jsName = %@  moduleName = %@", jsName, moduleName);
  dispatch_async(dispatch_get_main_queue(), ^{
    RootViewController *rootViewController = [RootViewController newViewCtlWithJsName:jsName moduleName:moduleName params:params];
    
    RCTViewDelegate *delegate = [ctlArray lastObject];
    
    rootViewController =[RootViewController presentFromViewController:delegate.viewController jsName:jsName moduleName:moduleName params:params];
    
    
  });
}

RCT_EXPORT_METHOD(dismissNow) {
  RCTViewDelegate *delegate = ctlArray.lastObject;
  [RootViewController dismissViewController:delegate.viewController];
}


+ (void)addViewDelegate:(RCTViewDelegate *)viewDelegate {
  if (ctlArray == nil) {
    ctlArray = [NSMutableArray array];
  }
  
  if ([ctlArray containsObject:viewDelegate]) {
    return;
  }
  
  [ctlArray addObject:viewDelegate];
  
}

+ (void)removeViewDelegate:(RCTViewDelegate *)viewDelegate {
  if (ctlArray == nil) {
    ctlArray = [NSMutableArray array];
  }
  [ctlArray removeObject:viewDelegate];
}

@end
